package com.roya.web.api;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.roya.common.ResultMap;
import com.roya.common.ResultMap.STATUS;
import com.roya.dto.NoticeDTO;
import com.roya.service.INoticeService;
import com.roya.util.DTOdateSet;

/**
 * 公告 action
 * @author fq
 *
 */
@Controller("noticeAction")
@RequestMapping(value="/api")
public class NoticeAction extends BaseAction{
	@Autowired
	private INoticeService noticeService;
	
	@ResponseBody
	@RequestMapping("noticeAdd.shtml")
	public ResultMap noticeAdd(HttpServletRequest request,HttpServletResponse response,NoticeDTO dto){
		log.info("in noticeAdd begin");
		ResultMap resultMap = new ResultMap();
		try {
		DTOdateSet.setCreateInfo(dto, request.getSession());
		DTOdateSet.setUpdateInfo(dto, request.getSession());
			int id=noticeService.addNotice(dto);
			resultMap.setAttribute("id",id);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			log.error("in noticeAdd end");
		} catch (SQLException e) {
		log.error("noticeAdd action is exception",e);
		resultMap.setStatus(STATUS.FAILE);
		resultMap.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("noticeList.shtml")
	public ResultMap noticeList(HttpServletRequest request,HttpServletResponse response,NoticeDTO dto){
		log.info("in noticeList begin");
		List<NoticeDTO> noticeList;
		ResultMap resultMap = new ResultMap();
		try {
			noticeList = noticeService.listNotice(dto);
			resultMap.setAttribute("list",noticeList);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			
		log.info("in noticeList end");
		} catch (SQLException e) {
			log.error("noticeList action is exception",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("noticeInfo.shtml")
	public ResultMap noticeInfo(HttpServletRequest request,HttpServletResponse response,NoticeDTO dto){
		log.info("in noticeInfo begin");
		ResultMap resultMap = new ResultMap();
		try {
			NoticeDTO noticeDTO = noticeService.infoNotice(dto);
			resultMap.setAttribute("dto",noticeDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			log.info("in noticeInfo end");
		} catch (SQLException e) {
			log.error("noticeInfo action is exception",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("noticeDel.shtml")
	public ResultMap interfaceInfoDel(HttpServletRequest request,HttpServletResponse response,NoticeDTO dto){
		log.info("in noticeDel begin");
		ResultMap resultMap = new ResultMap();
		try {
			noticeService.delNotice(dto);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			log.info("in noticeDel end");
		} catch (SQLException e) {
			log.error("noticeDel action is exception", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return resultMap;
	}
	
	@SuppressWarnings({"unchecked","rawtypes"})
	@ResponseBody
	@RequestMapping("noticeUpdate.shtml")
	public ResultMap noticeUpdate(HttpServletRequest request,HttpServletResponse response,NoticeDTO dto){
		log.info("in noticeUpdate begin");
		ResultMap resultMap = new ResultMap();
		try {
		DTOdateSet.setUpdateInfo(dto, request.getSession());
		Map map =  getParameterMap(request);
		map.put("updateDate", dto.getUpdateDate());
		noticeService.updateNotice(map);
		resultMap.setStatus(STATUS.SUCCESS);
		resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		log.info("in noticeUpdate end!!!");
		} catch (SQLException e) {
			log.error("noticeUpdate action is exception", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return resultMap;
	}
	/**
	 * 从request中获得参数Map，并返回可读的Map
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public  Map getParameterMap(HttpServletRequest request) {
	    // 参数Map
	    Map properties = request.getParameterMap();
	    // 返回值Map
	    Map returnMap = new HashMap();
	    Iterator entries = properties.entrySet().iterator();
	    Map.Entry entry;
	    String name = "";
	    String value = "";
	    while (entries.hasNext()) {
	        entry = (Map.Entry) entries.next();
	        name = (String) entry.getKey();
	        Object valueObj = entry.getValue();
	        if(null == valueObj){
	            value = "";
	        }else if(valueObj instanceof String[]){
	            String[] values = (String[])valueObj;
	            for(int i=0;i<values.length;i++){
	                value = values[i] + ",";
	            }
	            value = value.substring(0, value.length()-1);
	        }else{
	            value = valueObj.toString();
	        }
	        returnMap.put(name, value);
	    }
	    return returnMap;
	}
	
	@RequestMapping("/loginyq.shtml")
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) {
		
		return new ModelAndView("login");
	}

}
